//------------------------------------------------------------------------------
//
// 临时客户资源管理
// author: 喵大斯( as3er.net )
// created: 2015/9/18
// copyright (c) 2015 喵大斯( aosnow@yeah.net )
//
//------------------------------------------------------------------------------

/// <reference path='../core/jquery.d.ts'/>
/// <reference path='../core/page_core.d.ts'/>
/// <reference path="../core/submitter.d.ts"/>

//--------------------------------------------------------------------------
//
// variables
//
//--------------------------------------------------------------------------

/** 外部初始化方法 */
declare var init:Function;

// 无效客户资源数量
var invalid_total:number;

//--------------------------------------------------------------------------
//
// methods
//
//--------------------------------------------------------------------------

// 初始化
$( "body" ).ready( pageInit );

// 页面初始化
function pageInit()
{
	// 若外部需要设置初始化操作，则实现 init 名称的方法，即可被自动调用
	if( typeof( init ) == "function" ) init();

	// 提交到回收站
	$( "#btnSubmit" ).click( submitHandler );

	// 转移30天以前的到回收站
	$( "#btnMonth" ).click( submitHandler );

	// 清空回收站
	$( "#btnClean" ).click( submitHandler );
}

// 回收无效客户资源
function recycleInvalidClient( type:string )
{
	if( invalid_total <= 0 && (type == "move" || type == "month") )
	{
		infopad.err( "已经没有可转移的无效客户资源！" );
		return;
	}

	postHandler( type );
}

function postHandler( type:string )
{
	submitter.post( "system_client_temp_go.php", { key: type }, function( d )
	{
		if( d.err == 0 )
		{
			infopad.msg( d.msg );

			invalid_total = d.total;
			$( "#txtTotal" ).text( d.total );
			$( "#txtRecycle" ).text( d.recycle );
		}
		else
		{
			infopad.err( d.msg, d.delay );
		}
	}, "json" );
}

//--------------------------------------------------------------------------
//
// Event handlers
//
//--------------------------------------------------------------------------

function submitHandler( e ):void
{
	if( e.currentTarget == $( "#btnSubmit" ).get( 0 ) )
	{
		recycleInvalidClient( "move" );
	}
	else if( e.currentTarget == $( "#btnMonth" ).get( 0 ) )
	{
		recycleInvalidClient( "month" );
	}
	else
	{
		recycleInvalidClient( "clean" );
	}
}